/**
 * Project Name:sz_frame
 * File Name:RoleService.java
 * Package Name:com.sz.app.service.impl
 * Date:2015年8月13日下午1:30:30
 * Copyright (c) 2015, chenzhou1025@126.com All Rights Reserved.
 *
 */

package com.energy.analysis.sys.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.energy.analysis.comm.ConstantsBase;
import com.energy.analysis.comm.base.BaseMapper;
import com.energy.analysis.comm.base.BaseService;
import com.energy.analysis.comm.utils.StringUtils;
import com.energy.analysis.sys.entity.OprInfo;
import com.energy.analysis.sys.mapper.OprInfoMapper;

@Service
public class OprInfoService extends BaseService<OprInfo> {
	@Autowired
	private OprInfoMapper oprInfoMapper;

	@Override
	protected BaseMapper<OprInfo> getMapper() {
		return oprInfoMapper;
	}
	/**
	 * @Title: getByLoginId
	 * @Description: TODO(根据loginId 获取操作员信息)
	 * @param @param loginId
	 * @param @return 设定文件
	 * @return OperInfo 返回类型
	 * @throws
	 */
	public OprInfo getByLoginId(String loginId) {
		try {
			return oprInfoMapper.getByLoginId(loginId);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return null;
	}
	/**
	 * 
	 * @Title: isExistOperInfo
	 * @Description: TODO(是否存在当前LoginId)
	 * @param @param loginId
	 * @param @return 设定文件
	 * @return boolean 返回类型
	 * @throws
	 */ 
	public boolean isExistOprInfo(String loginId) {
		return oprInfoMapper.getCountByLoginId(loginId) < 1;
	}
	
	/**
	 * 
	 * @Title: save
	 * @Description: TODO(保存)
	 * @param @param oprInfo
	 * @param @return 设定文件
	 * @return boolean 返回类型
	 * @throws
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean save(OprInfo oprInfo) {
		// 获取ID数据
		// 密码加密
		if (StringUtils.isNotEmpty(oprInfo.getLoginPwd())) {
			oprInfo.setLoginPwd(oprInfo.encodePwd(oprInfo.getLoginPwd()));
		}
		// 添加
		Integer result = oprInfoMapper.insert(oprInfo);
		if (result > 0) {
			// 添加角色与操作员关系信息
			Set<String> roles = oprInfo.getRoles();
			if (roles != null && roles.size() > 0) {
				List<Map<String, Object>> roleList = setAddRoleToList(oprInfo.getOprId(), roles, ConstantsBase.OPR_ROLE_REL_01);
				oprInfoMapper.insertOprRoleRel(roleList);
			}
		}
		return result > 0;
	}
	private List<Map<String, Object>> setAddRoleToList(Integer oprId, Set<String> roles, String relType) {
			List<Map<String, Object>> roleList = new ArrayList<Map<String, Object>>(roles.size());
			for (String role : roles) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("oprId", oprId);
				map.put("relType", relType);
				map.put("roleId", role);
				roleList.add(map);
			}
			return roleList;
	}
    /**
	 * 更新
	 * 
	 * @Title: modify
	 * @Description: TODO(这里用一句话描述这个方法的作用)
	 * @param @param oprInfo
	 * @param @param rolesIds
	 * @param @param authRolesIds
	 * @param @return 设定文件
	 * @return Boolean 返回类型
	 * @throws
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	public Boolean modify(OprInfo oprInfo) {
		// 更新操作员信息
		if (oprInfo == null || oprInfoMapper.update(oprInfo) < 1) {
			// 更新失败
			return false;
		}
		// 密码加密
		if (StringUtils.isNotEmpty(oprInfo.getLoginPwd())) {
			oprInfo.setLoginPwd(new Md5Hash(oprInfo.getLoginPwd(), null).toString());
		}
		// 1更新角色信息 1.删除使用角色  3.添加使用角色 
		Map<String, Object> delRoleMap = new HashMap<String, Object>();
		delRoleMap.put("oprId", oprInfo.getOprId());
		delRoleMap.put("relType", ConstantsBase.OPR_ROLE_REL_01);
		oprInfoMapper.deleteOprRoleRel(delRoleMap);
		// 3 添加角色与操作员关系信息
		Set<String> roles = oprInfo.getRoles();
		if (roles != null && roles.size() > 0) {
			List<Map<String, Object>> roleList = setAddRoleToList(oprInfo.getOprId(), roles, ConstantsBase.OPR_ROLE_REL_01);
			oprInfoMapper.insertOprRoleRel(roleList);
		}
		// 返回结果
		return true;
	}
	/**
	 * 删除
	 * 
	 * @Title: del
	 * @Description: TODO(这里用一句话描述这个方法的作用)
	 * @param @return 设定文件
	 * @return Boolean 返回类型
	 * @throws
	 */ 
	public Boolean del(OprInfo oprInfo) {
		oprInfo.setOprSts(ConstantsBase.OPR_STS_00);
		return oprInfoMapper.update(oprInfo) > 0;
	}
	 /**
	 * 修改密码
	 * 
	 * @Title: modifyPwd
	 * @Description: TODO(这里用一句话描述这个方法的作用)
	 * @param @param oprInfo
	 * @param @return 设定文件
	 * @return Boolean 返回类型
	 * @throws
	 */
	public Boolean modifyPwd(OprInfo oprInfo) {
		// 密码加密
		if (oprInfo != null && StringUtils.isNotEmpty(oprInfo.getLoginPwd())) {
			oprInfo.setLoginPwd(oprInfo.encodePwd(oprInfo.getLoginPwd()));
			return oprInfoMapper.updatePwdByOprId(oprInfo) > 0;
		}
		return false;
	}
	
	public boolean modifyPassword(String oldPassword, String newPassword) {
		return modifyPassword(oldPassword, newPassword, null);
	}
	
	public boolean modifyPassword(String oldPassword, String newPassword, OprInfo oprInfo) {
		if (oprInfo == null) {
		 oprInfo =  (OprInfo)SecurityUtils.getSubject().getPrincipal();
		}
		if(!oprInfo.checkPwd(oldPassword)){
			return false;
		}
		// 修改密码
		String npassword = oprInfo.encodePwd(newPassword);
		Map<String, Object> paramMap = new HashMap<String,Object>();
		paramMap.put("password", npassword);
		paramMap.put("loginId", oprInfo.getLoginId());
		boolean b =  oprInfoMapper.modifyPassword(paramMap) > 0;
		if(b){
			oprInfo.setLoginPwd(npassword);
		}
		return b;
	}

	
}